package com.linewell.service.impl;

import com.linewell.dao.ArticleMapper;
import com.linewell.dao.CommentMapper;
import com.linewell.dao.UserInfoMapper;
import com.linewell.entity.Reply;
import com.linewell.entity.ReplyVo;
import com.linewell.entity.UserInfo;
import com.linewell.service.ICommentService;
import com.linewell.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Title: ssm2
 * @description:
 * @author: hyuhuang@linewell.com
 * @since:2018/2/21
 */
@Service
public class CommentService implements ICommentService {

    @Autowired
    private CommentMapper commentDao;
    @Autowired
    private ArticleMapper articleDao;
    @Autowired
    private UserInfoMapper userInfoDao;

    /**
     * @descripition 插入评论的数据
     * @author hyuhuang@linewell.com
     * @date 2018/2/27 10:02
     * @param articleId, content, userInfo
     * @return com.linewell.utils.Msg
     */
    public Msg insertComment(long articleId, String content, UserInfo userInfo,long articleByUserID) {
        Long id = System.currentTimeMillis();
        Long id2 = System.currentTimeMillis();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Reply reply = new Reply();
        reply.setReplyId(id2);
        reply.setCommentId(id);
        reply.setReplyDate(sdf.format(new Date()));
        reply.setReplyText(content);
        reply.setReplyType(0);
        UserInfo articleByUser = new UserInfo();
        articleByUser.setUserId(articleByUserID);
        reply.setReplyToId(articleByUser);
        reply.setReplyFromId(userInfo);
        reply.setReplyZan(0);
        reply.setReplyCai(0);
        reply.setIsCheck(0);
        commentDao.insertReply(reply);
        commentDao.insertComment(id,articleId,articleByUserID);
        return Msg.success().add("reply",reply);
    }


    public Msg getReply(long userId, long articleId,int page) {
        List<Reply> replyList = commentDao.getReply(articleId,page);
        List<List<Reply>> replies = new ArrayList<List<Reply>>();
        if (replyList.size()>0){
            for (Reply reply:replyList) {
                if (reply.getReplyToId().getUserId() == userId){
                    //全部显示
                    replies.add(getReply1(reply.getReplyId()));
                }else if (reply.getReplyFromId().getUserId() == userId){
                    //显示自己能看到的
                    replies.add(getReply1(reply.getReplyId()));
                }
            }
            return Msg.success().add("replies",replies).add("replyList",replyList);
        } else {
            return Msg.fail();
        }

    }


    public Msg getCommentByUserId(long userId, int page) {
        List<Reply> replyList = commentDao.getCommentByUserId(userId,page);
        return Msg.success().add("replyList",replyList);
    }

    public Msg deleteComment(long replyId) {
        if (commentDao.deleteComment(replyId)){
            return Msg.success();
        } else {
            return Msg.fail();
        }
    }

    public Msg godComment(long articleId) {
        Reply godComment = commentDao.godComment(articleId);
        if (godComment != null){
            return Msg.success().add("godComment",godComment);
        } else {
            return Msg.fail();
        }
    }

    public Msg getHotReply(int page) {
        List<Reply> replyList = commentDao.getHotReply(page);
        if (replyList.size()>0){
            return Msg.success().add("replyList",replyList);
        } else {
            return Msg.fail();
        }
    }

    public Msg insertReply(Long commentId, Long replyFromId, Long replyToId, String content) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Reply reply = new Reply(System.currentTimeMillis(),commentId,sdf.format(new Date()),1,content,0,0);
        UserInfo replyFromUser = userInfoDao.getUserById(replyFromId);
        UserInfo replyToUser = userInfoDao.getUserById(replyToId);
        reply.setReplyFromId(replyFromUser);
        reply.setReplyToId(replyToUser);

        commentDao.insertReply(reply);
        return Msg.success().add("reply",reply);
    }

    public  List<Reply> getReply1(long replyId){
        List<Reply> replies = new ArrayList<Reply>();
        List<Reply> replyFromReply = commentDao.getReplyFromReply(replyId);
        if (replyFromReply.size() == 0){
            return replies;
        } else {
            replies=getReply1(replyFromReply.get(0).getReplyId());
            replies.add(replyFromReply.get(0));
            return replies;
        }

    }

    public int getReplyCount() {
        return commentDao.getReplyCount();
    }

    public List<ReplyVo> getReplyList(int page, int limit) {
        return commentDao.getReplyList(page,limit);
    }

    public Msg updataReplyCheck(Long replyId) {
        commentDao.updataReplyCheck(replyId);
        return Msg.success();
    }

    public Msg deleteReplyByReplyId(Long replyId) {
        if (commentDao.deleteReplyByReplyId(replyId)){
            return Msg.success();
        } else {
            return Msg.fail();
        }

    }


    public Msg updataAllReply(String replyIds) {
        if (commentDao.updataAllReply(replyIds)){
            return Msg.success();
        } else {
            return Msg.fail();
        }
    }
}
